﻿/*
 * Copyright 2009 - 2010 eScience Institute
 *
 * This file is part of SqlShare.
 *
 * SqlShare is free software: you can redistribute it and/or modify it under the terms 
 * of the GNU General Public License as published by the Free Software Foundation, 
 * either version 3 of the License, or (at your option) any later version.
 *
 * SqlShare is distributed in the hope that it will be useful, but WITHOUT ANY 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with 
 * SqlShare. If not, see http://www.gnu.org/licenses/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Data;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using Microsoft.WindowsAzure.ServiceRuntime;
using SqlShareAPI;

namespace WebQueryRole
{
    public class CloudUtil
    {
        private const string STORAGE_ACCOUNT = "http://uwanthill.blob.core.windows.net/";
        private const string STORAGE_KEY = "Zfh1WuhgRnzl+x2WdaoE/NfYHHnA9Tt2Yh105cem/i7JpoyE9y7nhnxhJ0P35MJQ8wpiXmzm7yv0SJuOzZPLrA==";

        /// <summary>
        /// NOT READY, DO NOT USE
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static Stream GetDataFileStream(string filename)
        {
            StorageCredentialsAccountAndKey sc = new StorageCredentialsAccountAndKey(STORAGE_ACCOUNT, STORAGE_KEY);
            CloudStorageAccount storage = new CloudStorageAccount(sc, false);
            CloudBlobClient client = storage.CreateCloudBlobClient();

            CloudBlobContainer dir = client.GetContainerReference("datafiles");
            CloudBlob blob = dir.GetBlobReference("filename");

            BlobRequestOptions options = new BlobRequestOptions();            
            //blob.OpenWrite(
            return null;
        }

        public static SSAPIFactory CreateSSAPIFactory()
        {
            string contentStr, metaStr;
            if (RoleEnvironment.IsAvailable)
            {
                contentStr = RoleEnvironment.GetConfigurationSettingValue("ContentConnectionString");
                metaStr = RoleEnvironment.GetConfigurationSettingValue("MetaConnectionString");
            }
            else
            {
                contentStr = "Server=tcp:ec2-184-72-5-110.us-west-1.compute.amazonaws.com;User ID=web_user;Password=eScience4All!;Trusted_Connection=False;Database = SqlShare.v2.content;";
                metaStr = "Server=tcp:ec2-184-72-5-110.us-west-1.compute.amazonaws.com;User ID=web_user;Password=eScience4All!;Trusted_Connection=False;Database = SqlShare.v2.meta;";
            }

            return new SSAPIFactory(metaStr, contentStr);
        }
    }
}